﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Entity;
using Core.Dao;
using System.Data;
namespace Core.Business
{
    public class StaffBus
    {
        private StaffDao dao;
        public StaffBus() {
            dao = new StaffDao();
        }

        public bool insert(Staff staff) {
            return dao.insert(staff);
        }

        public bool update(Staff staff) {
            return dao.update(staff);
        }

        public bool delete(int id) {
            return dao.delete(id);
        }

        public bool delete(String ids) {
            return dao.delete(ids);
        }

        public Staff get(int id) {
            DataTable dt = dao.get(id);
            Staff staff = new Staff();

            if (dt.Rows.Count > 0)
            {
                staff.Id = Convert.ToInt32(dt.Rows[0]["STF_ID"]);
                staff.UserId = Convert.ToInt32(dt.Rows[0]["USR_ID"]);
                staff.Code = dt.Rows[0]["STF_CODE"].ToString();
                staff.FullName = dt.Rows[0]["STF_FULL_NAME"].ToString();
                staff.Gender = Convert.ToBoolean(dt.Rows[0]["STF_GENDER"]);
                staff.BirthDay = Convert.ToDateTime(dt.Rows[0]["STF_BIRTHDAY"]);
                staff.HireDate = Convert.ToDateTime(dt.Rows[0]["STF_HIRE_DATE"]);
                staff.Phone = dt.Rows[0]["STF_PHONE"].ToString();
                staff.Address = dt.Rows[0]["STF_ADDRESS"].ToString();
                staff.Desc = dt.Rows[0]["STF_DESC"].ToString();
            }
            return staff;
        }

        public List<Staff> getAll() {
            DataTable dt = dao.getAll();
            List<Staff> list = new List<Staff>();

            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    Staff staff = new Staff();
                    staff.Id = Convert.ToInt32(dr["STF_ID"]);
                    staff.UserId = Convert.ToInt32(dr["USR_ID"]);
                    staff.Code = dr["STF_CODE"].ToString();
                    staff.FullName = dr["STF_FULL_NAME"].ToString();
                    staff.Gender = Convert.ToBoolean(dr["STF_GENDER"]);
                    staff.BirthDay = Convert.ToDateTime(dr["STF_BIRTHDAY"]);
                    staff.HireDate = Convert.ToDateTime(dr["STF_HIRE_DATE"]);
                    staff.Phone = dr["STF_PHONE"].ToString();
                    staff.Address = dr["STF_ADDRESS"].ToString();
                    staff.Desc = dr["STF_DESC"].ToString();

                    list.Add(staff);
                }

            }
            return list;
        }

        public DataTable filter(string code, string fullName, string birthDay, string hireDay, string orderBy) {
            DataTable dt = dao.filter(code, fullName, birthDay, hireDay, orderBy);
            return dt;
        }

        public Staff getStaffByUserId(int userId)
        {
            DataTable dt = dao.getStaffByUserId(userId);
            Staff staff = new Staff();

            if (dt.Rows.Count > 0)
            {
                staff.Id = Convert.ToInt32(dt.Rows[0]["STF_ID"]);
                staff.UserId = Convert.ToInt32(dt.Rows[0]["USR_ID"]);
                staff.Code = dt.Rows[0]["STF_CODE"].ToString();
                staff.FullName = dt.Rows[0]["STF_FULL_NAME"].ToString();
                staff.Gender = Convert.ToBoolean(dt.Rows[0]["STF_GENDER"]);
                staff.BirthDay = Convert.ToDateTime(dt.Rows[0]["STF_BIRTHDAY"]);
                staff.HireDate = Convert.ToDateTime(dt.Rows[0]["STF_HIRE_DATE"]);
                staff.Phone = dt.Rows[0]["STF_PHONE"].ToString();
                staff.Address = dt.Rows[0]["STF_ADDRESS"].ToString();
                staff.Desc = dt.Rows[0]["STF_DESC"].ToString();
            }
            return staff;
        }

        public String issueNumber()
        {
            String pattern = "NV-000000";
            object curNumber = dao.getMaxNumber();
            int next;
            try
            {
                int num = Convert.ToInt32(curNumber);
                next = num + 1;
            }
            catch (Exception ex)
            {
                next = 1;
            };

            String nextNumber = pattern.Substring(0, pattern.Length - next.ToString().Length) + next;
            return nextNumber;
        }
    }
}
